import { listUniversity } from "@/api/end-metadata/university";
import { listDomain } from "@/api/end-metadata/domain";
import { listStyle } from "@/api/end-metadata/style";
import { listCadProvince } from "@/api/end-metadata/cad-province";
import { listCadCity } from "@/api/end-metadata/cad-city";
import { listCadCounty } from "@/api/end-metadata/cad-county";
import { listBank } from "@/api/end-metadata/bank";

export const metadataMixin = {
  data() {
    return {
      // 图片上传地址
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/core/auth-file/upload",
      highestEducationOptions: [
        {
          label: "高中",
          value: 1,
        },
        {
          label: "中专",
          value: 2,
        },
        {
          label: "大专",
          value: 3,
        },
        {
          label: "本科",
          value: 4,
        },
        {
          label: "硕士",
          value: 5,
        },
        {
          label: "博士",
          value: 6,
        },
        {
          label: "其他",
          value: 8,
        },
      ],
      workSeniorityOptions: [
        {
          label: "在校学生",
          value: 0,
        },
        {
          label: "3年以下",
          value: 1,
        },
        {
          label: "3-5年",
          value: 2,
        },
        {
          label: "5年以上",
          value: 3,
        },
      ],
      currentWorkingStatusOptions: [
        {
          label: "在职",
          value: 1,
        },
        {
          label: "待职",
          value: 2,
        },
      ],
      establishYearOptions: [
        {
          label: "1-3年",
          value: 1,
        },
        {
          label: "3-5年",
          value: 2,
        },
        {
          label: "5年以上",
          value: 3,
        },
      ],
      provinceOptions: [],
      cityOptions: [],
      countyOptions: [],
      schoolList: [],
      bankOptions: [],
      domainList: [],
      styleList: [],
    }
  },
  created() {
    this.getUniversity();
    this.getBankList();
    this.getDomainList();
    this.getStyleList();
    this.getProvinceList();
  },
  methods: {
    // 获取元数据及其处理
    getUniversity() {
      listUniversity().then((res) => {
        this.schoolList = res.list;
      });
    },
    getBankList() {
      listBank().then((res) => {
        this.bankOptions = res.list;
      });
    },
    getDomainList() {
      listDomain().then((res) => {
        this.domainList = res.list;
      });
    },
    getStyleList() {
      listStyle().then((res) => {
        this.styleList = res.rows;
        this.styleList.push({
          id: 0,
          name: "其他",
          description: "其他",
        });
      });
    },
    //省
    async getProvinceList() {
      const res = await listCadProvince();
      this.provinceOptions = res.list;
    },
    handleProvinceSelect(item) {
      const provinceId = item.provinceId;
      this.formData.location[0].provinceId = provinceId;
      this.formData.location[1].cityName = undefined;
      this.formData.location[2].countyName = undefined;
      listCadCity(provinceId).then((res) => {
        this.cityOptions = res.list;
      });
    },
    provinceSearch(queryString, cb) {
      let provinceOptions = this.provinceOptions.map(function (item) {
        return {
          value: item.name,
          provinceId: item.provinceId,
        };
      });
      let results = queryString
        ? provinceOptions.filter(this.provinceFilter(queryString))
        : provinceOptions;
      cb(results);
    },
    provinceFilter(queryString) {
      return (item) => {
        return item.value.includes(queryString) == 1;
      };
    },
    //市
    initCity(provinceId) {
      if(provinceId){
        listCadCity(provinceId).then((res) => {
          this.cityOptions = res.list;
        });
      }

    },
    citySearch(queryString, cb) {
      let cityOptions = this.cityOptions.map(function (item) {
        return {
          value: item.name,
          cityId: item.cityId,
        };
      });
      let results = queryString
        ? cityOptions.filter(this.cityFilter(queryString))
        : cityOptions;
      cb(results);
    },
    cityFilter(queryString) {
      return (item) => {
        return item.value.includes(queryString) == 1;
      };
    },
    async handleCitySelect(item) {
      const cityId = item.cityId;
      this.formData.location[1].cityId = cityId;
      this.formData.location[2].countyName = undefined;
      const res = await listCadCounty(cityId);
      this.countyOptions = res.list;
    },
    //县区
    handleCountySelect(item) {
      this.formData.location[2].countyId = item.countyId;
    },
    initCounty(cityId) {
     if(cityId){
       listCadCounty(cityId).then((res) => {
         this.countyOptions = res.list;
       });
     }
    },
    countySearch(queryString, cb) {
      let countyOptions = this.countyOptions.map(function (item) {
        return {
          value: item.name,
          countyId: item.countyId,
        };
      });
      let results = queryString
        ? countyOptions.filter(this.countyFilter(queryString))
        : countyOptions;
      cb(results);
    },
    countyFilter(queryString) {
      return (item) => {
        return item.value.includes(queryString) == 1;
      };
    },
    //学校
    schoolSearch(queryString, cb) {
      let schoolList = this.schoolList.map(function (item) {
        return {
          value: item.name,
          id: item.id,
        };
      });
      let results = queryString
        ? schoolList.filter(this.schoolFilter(queryString))
        : schoolList;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    schoolFilter(queryString) {
      return (schoolItem) => {
        console.log(schoolItem);
        return schoolItem.value.includes(queryString) == 1;
      };
    },
    // 地址信息处理
    formatFormData() {
      this.formData.location = JSON.stringify(this.formData.location);
    },
  }
}
